﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.BLL;
using System.Data;
using System.Globalization;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class HRDEvaluateDateConfig_POP : System.Web.UI.Page
    {
        private SsMng ssMg = new SsMng();
        private Main main = new Main();
        private HRDEvaluationBLL cmd = new HRDEvaluationBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int intRoundSeq = Convert.ToInt32(Request.QueryString["RoundSeq"]);
                string MODE = Request.QueryString["Mode"];
                
                LoadRound(intRoundSeq, MODE);
            }
        }

        private void LoadRound(int intRoundSeq, string MODE)
        {
            CultureInfo th = System.Globalization.CultureInfo.GetCultureInfo("th-TH");

            if (MODE == "Edit")
            {
                DataTable dtRound = cmd.Round(intRoundSeq);

                object Year = dtRound.Rows[0]["round_year"];
                txtRadYear.Text = Year == DBNull.Value ? "" : Year.ToString();

                object Round = dtRound.Rows[0]["round_no"];
                txtRadRound.Text = Round == DBNull.Value ? "" : Round.ToString();

                object RoundStart = dtRound.Rows[0]["round_start_date"];                
                string strRoundStart = Convert.ToDateTime(RoundStart.ToString()).ToString("dd/MM/yyyy",th);
                if(RoundStart == DBNull.Value)
                {
                    dtpRadRoundStart.SetValue("");
                }
                else 
                {
                    dtpRadRoundStart.SetValue(strRoundStart); 
                }
                                 
                object RoundEnd = dtRound.Rows[0]["round_end_date"];
                string strRoundEnd = Convert.ToDateTime(RoundEnd.ToString()).ToString("dd/MM/yyyy",th);
                if (RoundEnd == DBNull.Value)
                {
                    dtpRadRoundEnd.SetValue("");
                }
                else
                {
                    dtpRadRoundEnd.SetValue(strRoundEnd);
                }

                object PermissionStart = dtRound.Rows[0]["permission_start_date"];
                string strPermissionStart = Convert.ToDateTime(PermissionStart.ToString()).ToString("dd/MM/yyyy",th);
                if (PermissionStart == DBNull.Value)
                {
                    dtpRadPermissionStart.SetValue("");
                }
                else
                {
                    dtpRadPermissionStart.SetValue(strPermissionStart);
                }

                object PermissionEnd = dtRound.Rows[0]["permission_end_date"];
                string strPermissionEnd = Convert.ToDateTime(PermissionEnd.ToString()).ToString("dd/MM/yyyy",th);
                if (PermissionEnd == DBNull.Value)
                {
                    dtpRadPermissionEnd.SetValue("");
                }
                else
                {
                    dtpRadPermissionEnd.SetValue(strPermissionEnd);
                }

                object Remark = dtRound.Rows[0]["remark"];
                txtRemark.Text = Remark == DBNull.Value ? "" : Remark.ToString();

            }
            else if (MODE == "AddNew")
            {
                string strYear = Request.QueryString["Year"];                
                txtRadYear.Text = strYear;
            }
        }

        private bool OnValidationRound(string strYear, int intRoundNo)
        {
            DataTable dt = cmd.LoadRound(strYear);

            if (dt.Select(string.Format("round_no = {0}", intRoundNo)).Length > 0)
            {
                main.MessageAlert(Page, "รอบการประเมินมีอยู่แล้วในระบบ");
                return false;
            }

            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int intRoundSeq = Convert.ToInt32(Request.QueryString["RoundSeq"]);
            int intRound = Convert.ToInt32(Request.QueryString["RoundNo"]);
            string MODE = Request.QueryString["Mode"];

            if (MODE == "Edit")
            {
                int intYear = Convert.ToInt32(txtRadYear.Text);
                int intRoundNo = Convert.ToInt32(txtRadRound.Text);

                DateTime RoundStart = dtpRadRoundStart.GetDateValue();
                DateTime RoundEnd = dtpRadRoundEnd.GetDateValue();
                DateTime PermissionStart = dtpRadPermissionStart.GetDateValue();
                DateTime PermissionEnd = dtpRadPermissionEnd.GetDateValue();

                string strRemark = txtRemark.Text;
                                
                hdfYear.Value = intYear.ToString();

                if (intRoundNo == intRound)
                {
                    if (RoundEnd > RoundStart && PermissionEnd > PermissionStart)
                    {
                        cmd.UpdateRound(intRoundNo, intYear, RoundStart, RoundEnd, PermissionStart, PermissionEnd
                                        , strRemark, Convert.ToInt32(ssMg.EmpSeq), intRoundSeq);

                        Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                    }
                    else
                    {
                        main.MessageAlert(Page, "ตรวจสอบวันที่ของรอบการประเมิน หรือ วันที่อนุญาติประเมิน");
                    }

                }
                else if (OnValidationRound(intYear.ToString(), intRoundNo))
                {
                    cmd.UpdateRound(intRoundNo, intYear, RoundStart, RoundEnd, PermissionStart, PermissionEnd
                        , strRemark, Convert.ToInt32(ssMg.EmpSeq), intRoundSeq);

                    Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                }


            }
            else if (MODE == "AddNew")
            {
                int intYear = Convert.ToInt32(txtRadYear.Text);
                int intRoundNo = Convert.ToInt32(txtRadRound.Text);
                DateTime RoundStart = Convert.ToDateTime(dtpRadRoundStart.GetStringValue());
                DateTime RoundEnd = Convert.ToDateTime(dtpRadRoundEnd.GetStringValue());
                DateTime PermissionStart = Convert.ToDateTime(dtpRadPermissionStart.GetStringValue());
                DateTime PermissionEnd = Convert.ToDateTime(dtpRadPermissionEnd.GetStringValue());
                string strRemark = txtRemark.Text;

                hdfYear.Value = intYear.ToString();

                if (OnValidationRound(intYear.ToString(), intRoundNo))
                {
                    if (RoundEnd > RoundStart && PermissionEnd > PermissionStart)
                    {
                        cmd.CreateRound(intRoundNo, intYear, RoundStart, RoundEnd, PermissionStart, PermissionEnd
                                , strRemark, Convert.ToInt32(ssMg.EmpSeq));

                        Page.ClientScript.RegisterStartupScript(GetType(), "Close", "CloseRadWin();", true);
                    }
                    else
                    {
                        main.MessageAlert(Page, "ตรวจสอบวันที่ของรอบการประเมิน หรือ วันที่อนุญาติประเมิน");
                    }
                }
            }
        }
    }
}